package tk.leafotto.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.leafotto.ssm.beans.UserInfo;
import tk.leafotto.ssm.service.LoginService;
import tk.leafotto.ssm.utils.SHAUtils;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    LoginService loginService;
    @ResponseBody
    @RequestMapping("/login/{uname}/{pwd}")
    public String login(@PathVariable("uname") String username,@PathVariable("pwd") String pwd, HttpSession httpSession){
        UserInfo userInfo = new UserInfo();
        userInfo.setUname(username);
        userInfo.setPwd(SHAUtils.getSHA256Str(pwd));
        UserInfo loginInfo = loginService.login(userInfo);
        if(null != loginInfo){
            httpSession.setAttribute("user",userInfo);
            return loginInfo.getId() + "";
        }
        return "0";
    }
}
